<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: newsletter.php 72 2013-09-21 12:00:40Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 72 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:00:40 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

require_once(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.phpmailer.php');
require_once(DIR_FS_INC.'inc.php_mail.php');
if(MODULE_SEO_URL_INDEX_STATUS == 'True') {
	require_once(DIR_FS_INC.'inc.seo_url.php');
	!$seo_url ? $seo_url = new SeoUrl() : false;
} 

$smarty = new Smarty();

switch ($_GET['action']) {
    case 'save':
		$id = (int)$_POST['ID'];
		$status_all = $_POST['status_all'];
		
		if ($newsletter_title == '') 
			$newsletter_title = 'no title';
			
		$customers_status = get_customers_statuses();	 

		$rzp = '';
		for ($i=0,$n = sizeof($customers_status);$i<$n; $i++) {
			if ($_POST['status'][$i]=='yes') {
				if($rzp!='') 
					$rzp.=',';
				$rzp .= $customers_status[$i]['id'];
			}
		}

		if ($_POST['status_all']=='yes') 
			$rzp.=',all';

		$newsletter_personalize = $_POST['newsletter_personalize'];
		$newsletter_greeting = $_POST['newsletter_greeting'];
		$newsletter_gift = $_POST['newsletter_gift'];
		$newsletter_gift_ammount = $_POST['newsletter_gift_ammount'];

		if($_POST['newsletter_product_list'] != '') {
			$newsletter_product_list = $_POST['newsletter_product_list'];	  
		
			$sql_data_array = array('title' => $_POST['title'],
									'status' => '0',
									'bc' => $rzp,
									'cc' => $_POST['cc'],
									'date' => 'NOW()',
									'body' => stripslashes($_POST['newsletter_body']),
									'personalize' => $newsletter_personalize,
									'greeting' => $newsletter_greeting,
									'gift' => $newsletter_gift,
									'ammount' => $newsletter_gift_ammount,
									'product_list' => $newsletter_product_list);
		} else {	
			$sql_data_array = array('title' => $_POST['title'],
									'status' => '0',
									'bc' => $rzp,
									'cc' => $_POST['cc'],
									'date' => 'NOW()',
									'body' => stripslashes($_POST['newsletter_body']),
									'personalize' => $newsletter_personalize,
									'greeting' => $newsletter_greeting,
									'gift' => $newsletter_gift,
									'ammount' => $newsletter_gift_ammount);	  
		}
		
		if ($id != '') {
			$db->db_perform(TABLE_MODULE_NEWSLETTER, $sql_data_array, 'update', "newsletter_id = '".$id."'");

			$db->db_query("DROP TABLE IF EXISTS ".TABLE_MODULE_NEWSLETTER_TEMP."_".$id);
			$db->db_query("CREATE TABLE ".TABLE_MODULE_NEWSLETTER_TEMP."_".$id."(
							id int(11) NOT NULL auto_increment,
							customers_id int(11) NOT NULL default '0',
							customers_status int(11) NOT NULL default '0',
							customers_firstname varchar(64) NOT NULL default '',
							customers_lastname varchar(64) NOT NULL default '',
							customers_email_address text NOT NULL,
							mail_key varchar(32) NOT NULL,
							date datetime NOT NULL default '0000-00-00 00:00:00',
							comment varchar(64) NOT NULL default '',
							PRIMARY KEY(id))
							ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;");
		} else {
			$db->db_perform(TABLE_MODULE_NEWSLETTER, $sql_data_array);
			
			$id = $db->db_insert_id();
			$db->db_query("DROP TABLE IF EXISTS ".TABLE_MODULE_NEWSLETTER_TEMP."_".$id);
			$db->db_query("CREATE TABLE ".TABLE_MODULE_NEWSLETTER_TEMP."_".$id."(
							id int(11) NOT NULL auto_increment,
							customers_id int(11) NOT NULL default '0',
							customers_status int(11) NOT NULL default '0',
							customers_firstname varchar(64) NOT NULL default '',
							customers_lastname varchar(64) NOT NULL default '',
							customers_email_address text NOT NULL,
							mail_key varchar(32) NOT NULL,
							date datetime NOT NULL default '0000-00-00 00:00:00',
							comment varchar(64) NOT NULL default '',
							PRIMARY KEY(id))
							ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;");
		}

		$flag='';
		if (!strpos($rzp,'all')) 
			$flag = 'true';
		$rzp = str_replace(',all','',$rzp);
		$groups = explode(',',$rzp);
		$sql_data_array = '';
		
		for ($i=0,$n=sizeof($groups);$i<$n;$i++) {			
			if ($_POST['status_all']=='yes') {
				$customers = $db->db_query("SELECT
												customers_id,
												customers_firstname,
												customers_lastname,
												customers_email_address
											FROM 
												".TABLE_CUSTOMERS."
											WHERE
												customers_status='".$groups[$i]."'");
			} else {
				$customers = $db->db_query("SELECT
												customers_email_address,
												customers_id,
												customers_firstname,
												customers_lastname,
												mail_key        
											FROM 
												".TABLE_NEWSLETTER_RECIPIENTS."
											WHERE
												customers_status='".$groups[$i]."' 
											AND
												mail_status='1'");
			}
			while (!$customers->EOF){
				$sql_data_array = array('customers_id' => $customers->fields['customers_id'],
										'customers_status' => $groups[$i],
										'customers_firstname' => $customers->fields['customers_firstname'],
										'customers_lastname' => $customers->fields['customers_lastname'],
										'customers_email_address' => $customers->fields['customers_email_address'],
										'mail_key' => $customers->fields['mail_key'],
										'date' => 'NOW()');
				
				$db->db_perform(TABLE_MODULE_NEWSLETTER_TEMP.'_'.$id, $sql_data_array);
				$customers->MoveNext();
			}
		}
		redirect(href_link('newsletter.php'));
	break;
	
	case 'delete':
		$db->db_query("DELETE FROM ".TABLE_MODULE_NEWSLETTER." WHERE newsletter_id = '".(int)$_GET['ID']."'");
		redirect(href_link('newsletter.php'));
		break;
	
	case 'send':
		$package_size = '30';
		redirect(href_link('newsletter.php','send=0,'.$package_size.'&ID='.(int)$_GET['ID']));
		break;
}

if ($_GET['send']) {
	$limits = explode(',', $_GET['send']);
	$limit_low = $limits['0'];
	$limit_up = $limits['1'];
	
	$limit = $db->db_query("SELECT COUNT(*) as count FROM ".TABLE_MODULE_NEWSLETTER_TEMP."_".(int)$_GET['ID']."");
	
	$email = $db->db_query("SELECT
								n.customers_firstname,
								n.customers_lastname,
								n.customers_email_address,
								n.mail_key,
								n.id,
								c.customers_gender
							FROM 
								".TABLE_MODULE_NEWSLETTER_TEMP."_".(int)$_GET['ID']." n, 
								".TABLE_CUSTOMERS." c
							WHERE 
								n.customers_id = c.customers_id
							LIMIT ".$limit_low.",".$limit_up);
	
	$email_data = array();
	
	while(!$email->EOF) {
		$email_data[] = array('id' => $email->fields['id'],
								'firstname' => $email->fields['customers_firstname'],
								'lastname' => $email->fields['customers_lastname'],
								'email' => $email->fields['customers_email_address'],
								'key' => $email->fields['mail_key'],
								'customers_gender' => $email->fields['customers_gender']);
		$email->MoveNext();
	}
	
	$package_size = '30';
	$break = '0';
	
	if ($limit->fields['count'] < $limit_up) {
		$limit_up = $limit->fields['count'];
		$break='1';
	}
	
	$max_runtime = $limit_up-$limit_low;
	$newsletters = $db->db_query("SELECT * FROM ".TABLE_MODULE_NEWSLETTER." WHERE newsletter_id = '".(int)$_GET['ID']."'");
	
	require_once (DIR_FS_INC.'inc.get_tax_rate.php');
	require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.price.php');
	$price = new price(DEFAULT_CURRENCY, $_SESSION['customers_status']['customers_status_id']);
	
	$module_content = array();
	
	define('FILENAME_PRODUCT_INFO', 'product_info.php');
	
	$product = $db->db_query("	SELECT 
									* 
								FROM 
									".TABLE_NEWSLETTER_PRODUCTS." np, 
									".TABLE_PRODUCTS." p, 
									".TABLE_PRODUCTS_DESCRIPTION." pd
								WHERE 
									np.accessories_id = '".$newsletters->fields['product_list']."'
								AND 
									p.products_id = np.product_id
								AND 
									pd.products_id = p.products_id
								AND 
									pd.language_id = '".(int) $_SESSION['languages_id']."'");

	while(!$product->EOF) {
		$image = '';
		if ($product->fields['products_image'] != '')
			$image = HTTP_SERVER.DIR_WS_CATALOG.'images/product_images/thumbnail_images/'.$product->fields['products_image'];
		else
			$image = HTTP_SERVER.DIR_WS_CATALOG.'images/product_images/thumbnail_images/no_img.jpg';
		
		$fsk18 = '';
		if ($product->fields['products_fsk18'] == '1')
			$fsk18 = 'true';
		
		$price = $price->GetPrice($product->fields['products_id'], true, 1, $product->fields['products_tax_class_id'], $product->fields['products_price'], 1);
		if(MODULE_COMMERCE_SEO_INDEX_STATUS == 'True')
			$products_link = $seo_url->getProductLink('products_id='.$product->fields['products_id'], $connection, $_SESSION['languages_id']);
		else
			$products_link = product_link($product->fields['products_id'], $product->fields['products_name']);
		
		$module_content[] = array('PRODUCTS_NAME' => $product->fields['products_name'], 
									'PRODUCTS_MODEL' => $product->fields['products_model'], 
									'PRODUCTS_EAN' => $product->fields['products_ean'],
									'PRODUCTS_SHORT_DESCRIPTION' => $product->fields['products_short_description'], 
									'PRODUCTS_IMAGE' => $image, 
									'PRODUCTS_PRICE' => $price['formated'], 
									'PRODUCTS_LINK' => $products_link, 
									'PRODUCTS_FSK18' => $fsk18,
									'PRODUCTS_ID' => $product->fields['products_id']);
		$product->MoveNext();
	}
	
	for ($i=1;$i<=$max_runtime;$i++) {
		$link1 = chr(13).chr(10).chr(13).chr(10).TEXT_NEWSLETTER_REMOVE.chr(13).chr(10).chr(13).chr(10).HTTP_CATALOG_SERVER.DIR_WS_CATALOG.FILENAME_CATALOG_NEWSLETTER.'?action=remove&email='.$email_data[$i-1]['email'].'&key='.$email_data[$i-1]['key'];
		
		$link2 = '<br /><br />'.TEXT_NEWSLETTER_REMOVE.'<br /><a href="'.HTTP_CATALOG_SERVER.DIR_WS_CATALOG.FILENAME_CATALOG_NEWSLETTER.'?action=remove&email='.$email_data[$i-1]['email'].'&key='.$email_data[$i-1]['key'].'">'.TEXT_REMOVE_LINK.'</a>';
		
		if($newsletters->fields['gift'] == 'yes') {
			$id1 = create_coupon_code($email_data[$i-1]['email']);		
			$insert = $db->db_query("INSERT INTO ".TABLE_COUPONS." (coupon_code, coupon_type, coupon_amount, date_created) VALUES ('".$id1."', 'G', '".$newsletters_data->fields['ammount']."', now())");
			$insert_id = $db->db_insert_id();
			$insert = $db->db_query("INSERT INTO ".TABLE_COUPON_EMAIL_TRACK." (coupon_id, customer_id_sent, sent_firstname, emailed_to, date_sent) VALUES ('".$insert_id ."', '0', 'Admin', '".$email_data[$i-1]['email']."', now() )");
			
			$ammount = $newsletters->fields['ammount']." EUR";
			$gift_id = $id1;
		}
				
		$smarty->assign('background_path', HTTP_SERVER.DIR_WS_CATALOG.'images/newsletter/');
		$smarty->assign('body', $newsletters->fields['body']);
		$smarty->assign('gift_ammount', $ammount);
		$smarty->assign('gift_id', $gift_id);
		$smarty->assign('remove_link', $link2);
		$smarty->assign('shop_name',STORE_NAME);
		$smarty->assign('greeting_type', $newsletters->fields['greeting']);
		$smarty->assign('module_content', $module_content);
		
		$smarty->assign('customers_gender', $email_data[$i-1]['customers_gender']);
		$smarty->assign('personalize', $newsletters->fields['personalize']);
		$smarty->assign('customers_name', $email_data[$i-1]['firstname'].' '.$email_data[$i-1]['lastname']);  
		
		$smarty->caching = false;
		require_once (DIR_FS_INC.'inc.get_mail_body.php');
		$html_mail = $smarty->fetch('html:newsletter');
		$html_mail .= $signatur_html;
		$txt_mail = $smarty->fetch('txt:newsletter');
		$txt_mail .= $signatur_text;
		require_once (DIR_FS_INC.'inc.get_mail_data.php');
		$mail_data = get_mail_data('newsletter');
		
		
		$newletter_name = str_replace('{$shop}', STORE_NAME, $mail_data['EMAIL_ADDRESS_NAME']);
		$newletter_name = str_replace('{$date_today}', date('d.m.Y'), $newletter_name);
		
		php_mail($mail_data['EMAIL_ADDRESS'],
				$newletter_name, 
				$email_data[$i -1]['email'], 
				$email_data[$i -1]['lastname'].' '.$email_data[$i -1]['firstname'], 
				'', 
				$mail_data['EMAIL_REPLAY_ADDRESS'],
				$mail_data['EMAIL_REPLAY_ADDRESS_NAME'],
				'', 
				'', 
				$newsletters->fields['title'], 
				$html_mail, 
				$txt_mail);

		$db->db_query("UPDATE ".TABLE_MODULE_NEWSLETTER_TEMP."_".(int)$_GET['ID']." SET comment = 'send' WHERE id = '".$email_data[$i-1]['id']."'");
	}
	if ($break=='1') {
		$limit1_data = $db->db_query("SELECT 
											COUNT(*) AS count
										FROM 
											".TABLE_MODULE_NEWSLETTER_TEMP."_".(int)$_GET['ID']."
										WHERE 
											comment = 'send'");
		
		if($limit1_data->fields['count']-$limit_data['count'] <= 0) {
			$db->db_query("UPDATE ".TABLE_MODULE_NEWSLETTER." SET status = '1' WHERE newsletter_id = '".(int)$_GET['ID']."'");
			redirect(href_link('newsletter.php'));
			
		} else {
			$message_stack->add('<b>'.$limit1_data->fields['count'].'</b> Emails versendet', 'success');
			
			if($limit1_data->fields['count'] > 1)
				$message_stack->add('<b>'.$limit1_data->fields['count']-$limit_data['count'].'</b> fehlen noch', 'warning');
		}
	
	} else {
		$limit_low=$limit_up+1;
		$limit_up=$limit_low+$package_size;
		redirect(href_link('newsletter.php','send='.$limit_low.','.$limit_up.'&ID='.(int)$_GET['ID']));
	}
}

require(DIR_WS_INCLUDES.'metatag.php');

if(isset($_GET['action']) && ($_GET['action'] == 'new')) { ?>
<script type="text/javascript">
<!--
function checkIt(form){
	if (form.title.value != 0)
		return true;
	else {
		alert('Geben Sie einen Newletter Titel ein.');
		return false;
	}
}
//-->
</script>
<?php } ?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
<table class="outerTable" cellpadding="0" cellspacing="0">
	<tr>
		<td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
			<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
		</td>
		<td class="boxCenter" valign="top">
			<table border="0" width="100%" cellspacing="0" cellpadding="0">
				<tr>
					<td>
						<table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
							<tr>
								<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
							</tr>
						</table>
					</td>
				</tr>
		 		<?php if ($_GET['send']) { ?>
				<tr>
					<td>
						Sende...
					</td>
				</tr>
				<?php } ?>
				<tr>
					<td>
						<table width="100%" border="0">
							<tr>
								<td>
									<?php
									 // Default seite
									switch ($_GET['action']) {
									
										default:
											$customer_group_data = $db->db_query("SELECT
																						customers_status_name,
																						customers_status_id,
																						customers_status_image
																					FROM 
																						".TABLE_CUSTOMERS_STATUS."
																					WHERE
																						language_id='".$_SESSION['languages_id']."'");
											$customer_group=array();
											while (!$customer_group_data->EOF) {
												$group_data = $db->db_query("SELECT 
																					COUNT(*) AS count
																            	FROM 
																            		".TABLE_NEWSLETTER_RECIPIENTS."
																            	WHERE 
																            		mail_status='1' 
																            	AND
																            		customers_status='".$customer_group_data->fields['customers_status_id']."'");
												
												$customer_group[]=array( 'ID' => $customer_group_data->fields['customers_status_id'],
																          'NAME' => $customer_group_data->fields['customers_status_name'],
																          'IMAGE' => $customer_group_data->fields['customers_status_image'],
																          'USERS' => $group_data->fields['count']);
												
												$customer_group_data->MoveNext();
											} ?><br />
									
									<table width="100%" border="0" cellspacing="0" cellpadding="0">
										<tr>
											<td>
												<table width="100%" cellspacing="0" cellpadding="0" class="dataTable">
													<tr class="dataTableHeadingRow">
														<td class="dataTableHeadingContent" width="150" ><?php echo TITLE_CUSTOMERS; ?></td>
														<td class="dataTableHeadingContent last"  ><?php echo TITLE_STK; ?></td>
													</tr>
													<?php for ($i=0,$n=sizeof($customer_group); $i<$n; $i++) { ?>
													<tr>
														<td valign="middle" align="left">
															<?php echo image(DIR_WS_ICONS.$customer_group[$i]['IMAGE'], ''); ?><?php echo $customer_group[$i]['NAME']; ?>
														</td>
														<td align="left" class="last">
															<?php echo $customer_group[$i]['USERS']; ?>
														</td>
													</tr>
													<?php } ?>
												</table>
											</td>
											<td width="30%" align="right" valign="top"><?php
												echo '<a class="button" href="'.href_link('newsletter.php','action=new').'">'.BUTTON_NEW_NEWSLETTER.'</a>';
											?></td>
										</tr>
									</table><br />
									<?php
									$newsletters_data = $db->db_query("SELECT * FROM ".TABLE_MODULE_NEWSLETTER." WHERE status='0'");
									$news_data = array();
									while (!$newsletters_data->EOF) {
										$news_data[] = array('id' => $newsletters_data->fields['newsletter_id'],
															'date' => $newsletters_data->fields['date'],
															'title' => $newsletters_data->fields['title']);
										$newsletters_data->MoveNext();
									}
									?>
									<table width="100%" cellspacing="0" cellpadding="0" class="dataTable">
										<tr class="dataTableHeadingRow">
											<td class="dataTableHeadingContent" width="30" ><?php echo TITLE_DATE; ?></td>
											<td class="dataTableHeadingContent last" width="80%" ><?php echo TITLE_NOT_SEND; ?></td>
										</tr>
										<?php
										for ($i=0,$n=sizeof($news_data); $i<$n; $i++) {
											if ($news_data[$i]['id']!='') { ?>
										<tr>
											<td class="dataTableContent_products" align="left">
												<?php echo $news_data[$i]['date']; ?>
											</td>
											<td class="dataTableContent_products last" valign="middle" align="left">
												<a href="<?php echo href_link('newsletter.php','ID='.$news_data[$i]['id']); ?>">
													<b><?php echo $news_data[$i]['title']; ?></b>
												</a>
											</td>
										</tr>
										<?php
										if ($_GET['ID']!='' && $_GET['ID'] == $news_data[$i]['id']) {
											$total_data = $db->db_query("SELECT COUNT(*) AS count FROM ".TABLE_MODULE_NEWSLETTER_TEMP."_".(int)$_GET['ID'].""); ?>
										<tr>
											<td class="dataTableContent" valign="top" style="border-bottom: 1px solid; border-color: #999999;" align="left">
												<?php echo TEXT_SEND_TO.$total_data->fields['count']; ?><br /><br />
												<a class="button" href="<?php echo href_link('newsletter.php','action=delete&ID='.$news_data[$i]['id']); ?>" onclick="return confirm('<?php echo CONFIRM_DELETE; ?>')">
													<?php echo BUTTON_DELETE.'</a><br />'; ?>
												<a class="button" href="<?php echo href_link('newsletter.php','action=edit&ID='.$news_data[$i]['id']); ?>">
													<?php echo BUTTON_EDIT.'</a>'; ?>
											</td>
											<td colspan="2" class="dataTableContent" style="border-bottom: 1px solid #444; border-color: #999999; text-align: left;">
												<?php
												$newsletters_data = $db->db_query("SELECT * FROM ".TABLE_MODULE_NEWSLETTER." WHERE newsletter_id='".(int)$_GET['ID']."'");
												
												echo TEXT_TITLE.$newsletters_data->fields['title'].'<br />';
												$customers_status=get_customers_statuses();
												
												for ($i=0,$n=sizeof($customers_status);$i<$n; $i++) {
													$newsletters_data->fields['bc'] = str_replace($customers_status[$i]['id'],$customers_status[$i]['text'],$newsletters_data->fields['bc']);
												}
												
												echo TEXT_TO.$newsletters_data->fields['bc'].'<br />';
												echo TEXT_CC.$newsletters_data->fields['cc'].'<br /><br />';
												echo '<b>'.PERSONAL_NEWSLETTER.'</b>'.'<br />';
												
												if($newsletters_data->fields['personalize'] == 'yes') {
													echo 'personalisiert: <b>Ja</b><br />';
													if($newsletters_data->fields['greeting'] == '0')
														echo ANREDE.' h&ouml;flich<br />';
													else
														echo ANREDE.'pers&ouml;nlich<br />';
														
												} else
													echo 'personalisiert: Nein<br />';
												
												if($newsletters_data->fields['gift'] == 'yes') {
													echo 'Gutschein: <b>Ja</b><br />';	
													if($newsletters_data->fields['ammount'] > '0' && $newsletters_data->fields['gift'] == 'yes')
														echo 'Gutscheinwert: '.$newsletters_data->fields['ammount'].' EUR'.'<br />';
													else
														echo 'Gutscheinwert: 0<br /><br />';

												} else
													echo 'Gutschein: Nein<br /><br />';

												if($newsletters_data->fields['product_list'] != '0') {
													$actual_list = $db->db_query("SELECT list_name FROM ".TABLE_NEWSLETTER_PRODUCT_LIST." WHERE id = '".$newsletters_data->fields['product_list']."' ORDER BY list_name");
												
													echo 'Artikelliste: '.$actual_list->fields['list_name'].'<br /><br />';
												}
												?>
											</td>
										</tr>
										<tr>
											<td>
												<a class="button" href="<?php echo href_link('newsletter.php','action=send&ID='.$_GET['ID']); ?>"><?php echo BUTTON_SEND.'</a>'; ?>
											</td>
											<td>
												<table style="border-color: #cccccc; border: 1px solid #444;" width="100%">
													<tr>
														<td><?php echo $newsletters_data->fields['body'] ?></td>
													</tr>
												</table>
											</td>
										</tr>
										<?php }
											}
										}?>
									</table><br />
									<?php
									 $newsletters_data = $db->db_query("SELECT
																			newsletter_id,
																			date,title
																		FROM 
																			".TABLE_MODULE_NEWSLETTER."
																		WHERE 
																			status='1'");
									$news_data=array();
									while (!$newsletters_data->EOF) {
										$news_data[] = array('id' => $newsletters_data->fields['newsletter_id'],
										           			'date' => $newsletters_data->fields['date'],
										          			'title' => $newsletters_data->fields['title']);
										$newsletters_data->MoveNext();
									}
									?>
									<table border="0" width="100%" cellspacing="0" cellpadding="2">
								        <tr class="dataTableHeadingRow">
								          <td class="dataTableHeadingContent" width="80%" ><?php echo TITLE_SEND; ?></td>
								          <td class="dataTableHeadingContent" align="right"><?php echo TITLE_ACTION; ?></td>
								        </tr>
										<?php
										for ($i=0,$n=sizeof($news_data); $i<$n; $i++) {
											if ($news_data[$i]['id']!='') { ?>
											<tr>
												<td class="dataTableContent" valign="middle" align="left">
													<?php echo $news_data[$i]['date'].' '; ?><b><?php echo $news_data[$i]['title']; ?></b>
												</td>
												<td class="dataTableContent" align="right">
													<a href="<?php echo href_link('newsletter.php','action=edit&ID='.$news_data[$i]['id']); ?>">
													<?php echo image(DIR_WS_ICONS.'document--pencil.png',TEXT_EDIT,'','','style="cursor:pointer"').'</a>'; ?>
													<a href="<?php echo href_link('newsletter.php','action=delete&ID='.$news_data[$i]['id']); ?>" onclick="return confirm('<?php echo CONFIRM_DELETE; ?>')">
													<?php echo image(DIR_WS_ICONS.'delete.gif', TEXT_DELETE,'','','style="cursor:pointer" onclick="return confirm(\''.DELETE_ENTRY.'\')"').'</a> '; ?>
												</td>
											</tr>
										<?php
											}
										} ?>
									</table>
									<?php
										break;       // end default page
		
		  							case 'edit':  
		  
										$newsletters_data = $db->db_query("SELECT * FROM ".TABLE_MODULE_NEWSLETTER." WHERE newsletter_id='".(int)$_GET['ID']."'");
										
										$product_list_array = array (array ('id' => '', 'text' => TEXT_NONE));
										$product_list = $db->db_query("SELECT id, list_name FROM ".TABLE_NEWSLETTER_PRODUCT_LIST." ORDER BY list_name");
										
										while (!$product_list->EOF) {
											$product_list_array[] = array ('id' => $product_list->fields['id'], 'text' => $product_list->fields['list_name']);
											$product_list->MoveNext();
										}
										
									case 'safe':
									case 'new': 
										
										$product_list_array = array (array ('id' => '', 'text' => TEXT_NONE));
										$product_list = $db->db_query("SELECT id, list_name from ".TABLE_NEWSLETTER_PRODUCT_LIST." ORDER BY list_name");
										while (!$product_list->EOF) {
											$product_list_array[] = array ('id' => $product_list->fields['id'], 'text' => $product_list->fields['list_name']);
											$product_list->MoveNext();
										}
		
										$customers_status = get_customers_statuses();

		  								echo draw_form('edit_newsletter','newsletter.php','action=save','post','enctype="multipart/form-data" onsubmit="return checkIt(this);"').draw_hidden_field('ID',$_GET['ID']); ?>
									<br /><br />
									<table class="main" width="100%" border="0">
										<tr>
											<td width="10%"><?php echo TEXT_TITLE; ?></td>
											<td width="90%"><?php echo draw_input_field('title',$newsletters_data->fields['title'],'size=100'); ?></td>
										</tr>
										<tr>
											<td width="10%"><?php echo TEXT_TO; ?></td>
											<td width="90%"><?php
											for ($i=0,$n=sizeof($customers_status);$i<$n; $i++) {
											
											     $group_data = $db->db_query("SELECT 
																				COUNT(*) as count
																			FROM 
																				".TABLE_NEWSLETTER_RECIPIENTS."
																			WHERE 
																				mail_status='1'
																			AND
																				customers_status='".$customers_status[$i]['id']."'");
											
											     $group_data_all = $db->db_query("SELECT 
																					COUNT(*) AS count
																				FROM 
																					".TABLE_CUSTOMERS."
																				WHERE
																					customers_status='".$customers_status[$i]['id']."'");
											     
											     $bc_array = explode(',', $newsletters_data->fields['bc']);
											
												echo draw_checkbox_field('status['.$i.']','yes', in_array($customers_status[$i]['id'], $bc_array)).' '.$customers_status[$i]['text'].'  <i>(<b>'.$group_data->fields['count'].'</b>'.TEXT_USERS.$group_data_all->fields['count'].TEXT_CUSTOMERS.'<br />';
											}
											echo draw_checkbox_field('status_all', 'yes',in_array('all', $bc_array)).' <b>'.TEXT_NEWSLETTER_ONLY.'</b>';
											?></td>
		   								</tr>
										<tr>
											<td width="10%"><?php echo TEXT_CC; ?></td>
											<td width="90%"><?php echo draw_input_field('cc',$newsletters_data->fields['cc'],'size=100'); ?></td>
										</tr>
		     							<?php $greeting_array = array(array('id' => 0,'text' => 'h&ouml;fliche Anrede'),array('id' => 1,'text' => 'pers&ouml;nliche Anrede')); ?>
										<tr>
											<td width="10%">&nbsp;</td>
											<td width="90%">
												<table width="100%" class="main" style="border:solid 1px #cccccc; background-color:#f1f1f1;">
													<tr>		    
														<td colspan="2"><b>Personalisierbarer Newsletter:</b></td>
													</tr>	
													<tr>		    
														<td align="left" width="180">Newsletter personalisieren:</b></td>
														<td align="left"><?php echo draw_checkbox_field('newsletter_personalize', 'yes', $newsletters_data->fields['personalize']); ?></td>
													</tr>
													<tr>		    
														<td align="left" width="180">&nbsp;</td>
														<td align="left"><i>Sie haben die M&ouml;glichkeit, den Newsletter pers&ouml;nlicher zu gestalten, in dem Sie den Kunden mit seinem Namen ansprechen.</i></td>
													</tr>
													<tr>		    
														<td colspan="2"><b>Optionen:</b></td>
													</tr>	
													<tr>		    
														<td align="left" width="180">Anrede:</td>
														<td align="left"><?php echo draw_pull_down_menu('newsletter_greeting', $greeting_array, $newsletters_data->fields['greeting']); ?></td>
													</tr>
													<tr>
														<td align="left" width="180">&nbsp;</td>
														<td align="left"><i>h&ouml;fliche Anrede = Sehr geehrter Herr Mustermann</i></td>
													</tr>
													<tr>
														<td align="left" width="180">&nbsp;</td>
														<td align="left"><i>pers&ouml;nliche Anrede = Hallo Herr Mustermann</i></td>
													</tr>
													<tr>		    
														<td colspan="2"><b>Gutschein:</b></td>
													</tr>
													<tr>		    
														<td align="left" width="180">Gutschein einf&uuml;gen:</td>
														<td align="left"><?php echo draw_checkbox_field('newsletter_gift', 'yes', $newsletters_data->fields['gift']).' <i>(Aktivieren Sie diese Funktion, wenn Sie dem Newsletter einen Gutschein beilegen m&ouml;chten)</i>'; ?></td>
													</tr>
													<tr>		    
														<td align="left" width="180">Gutscheinwert:</td>
														<td align="left"><?php echo draw_input_field('newsletter_gift_ammount',$newsletters_data->fields['ammount'],'size=5').' EUR <i>(Gutscheinwert in EUR. z.B.: 20.00)</i>'; ?></td>
													</tr>
													<tr>		    
														<td colspan="2">&nbsp;</td>
													</tr>
													<tr>		    
														<td colspan="2"><b>Artikelliste einf&uuml;gen:</b></td>
													</tr>
													<?php if($newsletters_data->fields['product_list'] != '0') { 
														$actual_list = $db->db_query("SELECT list_name from ".TABLE_NEWSLETTER_PRODUCT_LIST." WHERE id = '".$newsletters_data->fields['product_list']."' ORDER BY list_name");
													?>
													<tr>		    
														<td align="left" width="180">ausgew&auml;hlte Artikelliste:</td>
														<td align="left"><?php echo $actual_list->fields['list_name']; ?></td>
													</tr>
													<tr>		    
														<td align="left" width="180">neue Artikelliste:</td>
														<td align="left"><?php echo draw_pull_down_menu('newsletter_product_list', $product_list_array, $product_list_array); ?></td>
													</tr>
													<?php } else { ?>
													<tr>		    
														<td align="left" width="180">Artikelliste:</td>
														<td align="left"><?php echo draw_pull_down_menu('newsletter_product_list', $product_list_array, $product_list_array); ?></td>
													</tr>
													<?php } ?>
												</table>	  
											</td>      
										</tr>
										<tr>
											<td width="10%" valign="top"><?php echo TEXT_BODY; ?></td>
											<td width="90%">
												<?php echo wysiwyg('newsletter_body', stripslashes($newsletters_data->fields['body']));	?>
											</td>
										</tr>
									</table>
									<a class="button" onclick="this.blur();" href="<?php echo href_link('newsletter.php'); ?>"><?php echo BUTTON_BACK; ?></a>
									<button type="submit" class="button" onclick="this.blur();"><?php echo BUTTON_SAVE ?></button>
								</form>
								<?php
								break;
										} // end switch
										?>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>